const express = require('express')
const {
    getCategoryProductListByNumber,
    getBrandProductListByNumber,
    getProductDetail,
    getProductListByState,
    getProductListByNumber,
    getProductOrderByPrice,
    getProductOrderBySell,
    getProductOrderByDiscount,
    filterProductList,
    searchProductList
} = require('../../model/mongo')
const router = express.Router()

router.get('/list', (req, res) => {
    let { count, limit } = req.query
    getProductListByNumber({ count }).then(data => {
        res.send(JSON.stringify({
            code: 200,
            msg: '分页获取商品列表',
            data,
        }))
    }).catch(err => {
        res.send(JSON.stringify({
            code: 400,
            msg: '获取商品失败',
            err,
        }))
    })
})

router.get('/detail/:proid', async (req, res) => {
    const { proid } = req.params
    const result = {
        code: 200,
        msg: '获取商品详情'
    }
    result.data = await getProductDetail(proid)
    res.send(JSON.stringify(result))
})

router.get('/sellbest', async (req, res) => {
    const data = await getProductOrderBySell(-1)
    res.send(JSON.stringify({
        code: 200,
        msg: '获取热销top10',
        data: data.slice(0, 10)
    }))
})

router.get('/seckill', (req, res) => {
    getProductListByState('isseckill', true).then(data => {
        res.send(JSON.stringify({
            code: 200,
            msg: '获取秒杀列表top10',
            data: data.slice(0, 10)
        }))
    })
})

router.get('/new', (req, res) => {
    getProductListByNumber({ count: 1, limit: 10 }).then(data => {
        res.send(JSON.stringify({
            code: 200,
            msg: '获取上新商品列表top10',
            data,
        }))
    }).catch(err => {
        res.send(JSON.stringify({
            code: 400,
            msg: '获取上新商品失败',
            err,
        }))
    })
})

router.get('/recomend', (req, res) => {
    getProductListByState('isrecommend', true).then(data => {
        res.send(JSON.stringify({
            code: 200,
            msg: '获取推荐列表top10',
            data: data.slice(0, 10)
        }))
    })
})

router.get('/getListByCategory', async (req, res) => {
    let { nin, category, limit } = req.query
    const result = {
        code: 200,
        msg: '分页获取' + category + '分类下的数据',
        nin: nin || [],
        data: {}
    }
    try {
        result.data = await getCategoryProductListByNumber({ nin: nin && eval(nin) || [], category })
    } catch (err) {
        result.msg = err
        result.code = 400
    }
    res.send(JSON.stringify(result))
})

router.get('/getListByBrand', async (req, res) => {
    let { nin, brand, limit } = req.query
    const result = {
        code: 200,
        msg: '分页获取' + brand + '品牌下的数据',
        data: {}
    }
    try {
        result.data = await getBrandProductListByNumber({ nin: nin && eval(nin) || [], brand })
    } catch (err) {
        result.msg = err
        result.code = 400
    }
    res.send(JSON.stringify(result))
})

router.get('/getListOrderBySell', async (req, res) => {
    const { sales, category } = req.query// 1 -1
    const data = await getProductOrderBySell(sales, category)
    res.send(JSON.stringify({
        code: 200,
        msg: '获取按照销量排序商品列表',
        data
    }))
})

router.get('/getListOrderByPrice', async (req, res) => {
    const { originprice, category } = req.query// 1 -1
    const data = await getProductOrderByPrice(originprice, category)
    res.send(JSON.stringify({
        code: 200,
        msg: '获取按照折后价格排序商品列表',
        data
    }))
})

router.get('/getListOrderByDiscount', async (req, res) => {
    const { discount, category } = req.query// 1 -1
    const data = await getProductOrderByDiscount(discount, category)
    res.send(JSON.stringify({
        code: 200,
        msg: '获取按照折扣排序商品列表',
        data
    }))
})

router.get('/filter', async (req, res) => {
    const { brand, category, lowerPrice, higherPrice } = req.query
    const filter = {}
    const result = {
        code: 200,
        msg: '获取筛选数据',
        data: []
    }
    if (brand && brand !== '全部品牌') {
        filter.brand = brand
    }
    if (category && category !== '全部商品') {
        filter.category = category
    }
    result.data = await filterProductList(filter)
    if (Number(lowerPrice)) {
        result.data = result.data.filter(item => Math.floor(item.originprice * item.discount / 10) >= lowerPrice)
    }
    if (Number(higherPrice)) {
        result.data = result.data.filter(item => Math.floor(item.originprice * item.discount / 10) <= higherPrice)
    }
    res.send(JSON.stringify(result))
})

router.get('/search', async (req, res) => {
    const { value } = req.query
    const result = {
        code: 200,
        msg: '搜索商品',
        data: []
    }
    result.data = await searchProductList(value)
    res.send(JSON.stringify(result))
})

module.exports = router
